//
//  DictionaryNullSafe.m
//  JunKeDai
//
//  Created by admin on 2018/3/13.
//  Copyright © 2018年 DD. All rights reserved.
//

#import <Foundation/Foundation.h>

@implementation NSDictionary (NullSafe)

+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceMethod:@selector(initWithObjects:forKeys:count:) with:@selector(ll_initWithObjects:forKeys:count:)];
        [self swizzleClassMethod:@selector(dictionaryWithObjects:forKeys:count:) with:@selector(ll_dictionaryWithObjects:forKeys:count:)];
    });
}

-(instancetype)ll_initWithObjects:(id  _Nonnull const [])objects forKeys:(id<NSCopying>  _Nonnull const [])keys count:(NSUInteger)cnt
{
    id safeObjects[cnt];
    id safeKeys[cnt];
    NSUInteger j = 0;
    for (NSUInteger i = 0; i < cnt; i++) {
        id key = keys[i];
        id obj = objects[i];
        if (!key) {
            continue;
        }
        if (!obj) {
            obj = [NSNull null];
            continue;
        }
        if ([obj isKindOfClass:[NSNull class]]) {
            continue;
        }
        safeKeys[j] = key;
        safeObjects[j] = obj;
        j++;
    }
    return [self ll_initWithObjects:safeObjects forKeys:safeKeys count:j];
}

+(instancetype)ll_dictionaryWithObjects:(id  _Nonnull const [])objects forKeys:(id<NSCopying>  _Nonnull const [])keys count:(NSUInteger)cnt
{
    id safeObjects[cnt];
    id safeKeys[cnt];
    NSUInteger j = 0;
    for (NSUInteger i = 0; i < cnt; i++) {
        id key = keys[i];
        id obj = objects[i];
        if (!key) {
            continue;
        }
        if (!obj) {
            obj = [NSNull null];
            continue;
        }
        if ([obj isKindOfClass:[NSNull class]]) {
            continue;
        }
        safeKeys[j] = key;
        safeObjects[j] = obj;
        j++;
    }
    return [self ll_dictionaryWithObjects:safeObjects forKeys:safeKeys count:j];
}
@end


@implementation NSMutableDictionary (NullSafe)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = NSClassFromString(@"__NSDictionaryM");
        [class swizzleInstanceMethod:@selector(setObject:forKey:) with:@selector(ll_setObject:forKey:)];
        [class swizzleInstanceMethod:@selector(setObject:forKeyedSubscript:) with:@selector(ll_setObject:forKeyedSubscript:)];
    });
}

- (void)ll_setObject:(id)anObject forKey:(id<NSCopying>)aKey {
    if (!aKey) {
        return;
    }
    if (!anObject) {
        anObject = [NSNull null];
//        [self removeObjectForKey:aKey];
    } else {
        if (![anObject isKindOfClass:[NSNull class]]) {
            [self ll_setObject:anObject forKey:aKey];
        }
    }
    
}

- (void)ll_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
    if (!key) {
        return;
    }
    if (!obj) {
        obj = [NSNull null];
    } else {
        if (![obj isKindOfClass:[NSNull class]]) {
            if ([obj isKindOfClass:[NSString class]]) {
                NSString *str = (NSString *)obj;
                if (str.length > 0) {
                    [self ll_setObject:obj forKey:key];
                }
            } else if ([obj isKindOfClass:[NSDictionary class]] || [obj isKindOfClass:[NSArray class]]) {
                if ([obj count] > 0) {
                    [self ll_setObject:obj forKey:key];
                }
            } else {
                [self ll_setObject:obj forKey:key];
            }
        }
    }
    
}

@end

